﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using CsGL.OpenGL;
using CsGL.Util;
using System.Diagnostics;
using System.Collections;

namespace vfife_building_algorithm
{
    class MyOpenGL : OpenGLControl  //To display the OpenGL window, creating a need to inherit from the CsGL.OpenGL.OpenGLControl class 
    {
        public bool isOrtho;
        
        protected override void InitGLContext()  // the following reconstruction
        {
            base.InitGLContext();
            GL.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
            GL.glColor3f(255, 0, 0);
            GL.glMatrixMode(GL.GL_PROJECTION);
            GL.glLoadIdentity();

            if (isOrtho)
            {
                GL.glOrtho(3, -3, -3, 3, 0, 100);
            }
            else
            {
                GL.gluPerspective(65.0f, Size.Width / Size.Height, 1.0f, 25.0f);
            }

            GL.glMatrixMode(GL.GL_MODELVIEW);
            GL.glLoadIdentity();

            GL.glMatrixMode(GL.GL_MODELVIEW);
            GL.glLoadIdentity();

            GL.glViewport(0, 0, Size.Width, Size.Height);
        }
        protected override void OnCreateControl()  // the following reconstruction
        {
            //GL.glEnable(GL.GL_DEPTH_TEST);
            GL.glClearDepth(1.0f);//parameter stands for distance from z axis,between 0 and 1

            GL.glDepthFunc(GL.GL_LEQUAL);
            GL.glEnable(GL.GL_DEPTH_TEST);
        }
        public void changeOthro()
        {

            GL.glMatrixMode(GL.GL_PROJECTION);
            GL.glLoadIdentity();
            if ((Size.Width != 0) && (Size.Height != 0))
            {
                if (isOrtho)
                {
                    GL.glOrtho(3, -3, -3, 3, 0, 100);
                }
                else
                {
                    GL.gluPerspective(65.0f, Size.Width / Size.Height, 1.0f, 25.0f);
                }
            }

            GL.glMatrixMode(GL.GL_MODELVIEW);
            GL.glLoadIdentity();
            GL.glViewport(0, 0, this.Size.Width, this.Size.Height);
        }
        
        protected override void OnSizeChanged(EventArgs e) // the following reconstruction
        {

            base.OnSizeChanged(e);

            GL.glColor3f(255, 0, 0);
            GL.glMatrixMode(GL.GL_PROJECTION);
            GL.glLoadIdentity();
            if ((Size.Width != 0) && (Size.Height != 0))
            {
                if (isOrtho)
                {
                    GL.glOrtho(3, -3, -3, 3, 0, 100);
                }
                else
                {
                    GL.gluPerspective(65.0f, Size.Width / Size.Height, 1.0f, 25.0f);
                }
            }

            GL.glMatrixMode(GL.GL_MODELVIEW);
            GL.glLoadIdentity();
            GL.glViewport(0, 0, this.Size.Width, this.Size.Height);

        }

        
        public override void glDraw()  // the following reconstruction:
        {
            GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
            GL.glLoadIdentity();

            GL.glTranslatef(-2.5f, -1.5f, -6.0f);

            //GL.glTranslatef(-1.0f, -0.5f, -6.0f);

            foreach (DictionaryEntry de in Model.Elements)
            {
                Node value0 = ((Element)de.Value).node1;
                Node value1 = ((Element)de.Value).node2;
                //Node value2 = e1.node1;
                //Node value3 = e1.node2;

                GL.glColor3f(163f, 148f, 128f);
                GL.glLineWidth(5.5f);
                GL.glBegin(GL.GL_LINES);
                //GL.glLineWidth(1.5f);

                GL.glVertex3f((float)(value0.Xcoord) / 2, (float)(value0.Ycoord) / 2, 0.0f);

                GL.glVertex3f((float)(value1.Xcoord) / 2, (float)(value1.Ycoord) / 2, 0.0f);
                GL.glEnd();
                GL.glFlush();
            }

        }
    }
}